package cn.flying.cloud.base.common.utils.network;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.flying.cloud.base.common.constants.Constants;
import cn.flying.cloud.base.common.utils.Ft;

/**
 * Servlet工具类
 *
 * @author: admin
 * @date: 2024年09月27日 10:01
 * @version: 1.0
 */
public class ServletUtil {

    /**
     * 获取HttpServletRequest
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        try {
            return getRequestAttributes().getRequest();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取header参数
     *
     * @param name
     * @return
     */
    public static String getHeader(String name) {
        if (getRequest() != null) {
            return getRequest().getHeader(name);
        }
        return null;
    }

    /**
     * 获取session
     *
     * @return
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 获取请求属性
     *
     * @return
     */
    public static ServletRequestAttributes getRequestAttributes() {
        try {
            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
            return (ServletRequestAttributes) attributes;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取请求headers
     *
     * @param request
     * @return
     */
    public static Map<String, String> getHeaders(HttpServletRequest request) {
        Map<String, String> map = new LinkedHashMap<>();
        Enumeration<String> enumeration = request.getHeaderNames();
        if (enumeration != null) {
            while (enumeration.hasMoreElements()) {
                String key = enumeration.nextElement();
                String value = request.getHeader(key);
                map.put(key, value);
            }
        }
        return map;
    }

    /**
     * 获取Token
     *
     * @param request
     * @return
     */
    public static String getToken(HttpServletRequest request) {
        //从头部获取token信息
        String token = request.getHeader(HttpHeaders.AUTHORIZATION);
        if (Ft.String.isBlank(token)) {
            token = request.getHeader(Constants.ACCESS_TOKEN);
        }
        //如果请求头部没有获取到token，则从请求的参数中进行获取
        if (Ft.String.isBlank(token)) {
            token = request.getParameter(Constants.ACCESS_TOKEN);
        }
        if (Ft.String.isBlank(token)) {
            String queryString = request.getQueryString();
            Map<String, String> queryParams = new HashMap<>();
            if (queryString != null) {
                String[] params = queryString.split("&");
                for (String param : params) {
                    String[] keyValue = param.split("=");
                    queryParams.put(keyValue[0], keyValue.length > 1 ? keyValue[1] : "");
                }
            }
            token = queryParams.get("access_token");
        }
        return token;
    }
}
